package com.lxj.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxj.domain.ProdTagReference;
import com.lxj.domain.Sku;
import com.lxj.service.ProdTagReferenceService;
import com.lxj.service.ProdTagService;
import com.lxj.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxj.domain.Prod;
import com.lxj.mapper.ProdMapper;
import com.lxj.service.ProdService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @author lxj
 * @date 2021/8/1 11:11
 * @Description:
 */
@Service
@Slf4j
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod> implements ProdService {

    @Autowired
    private ProdMapper prodMapper;

    @Autowired
    private ProdTagReferenceService prodTagReferenceService;

    @Autowired
    private SkuService skuService;

    /**
     * 分页查询商品
     *
     * @param page
     * @param prod
     * @return
     */
    @Override
    public Page<Prod> loadProdPage(Page<Prod> page, Prod prod) {
        return prodMapper.selectPage(page, new LambdaQueryWrapper<Prod>()
                .eq(!ObjectUtils.isEmpty(prod.getStatus()), Prod::getStatus, prod.getStatus())
                .like(StringUtils.hasText(prod.getProdName()), Prod::getProdName, prod.getProdName())
                .orderByDesc(Prod::getCreateTime)
        );
    }

    /**
     * 新增商品
     * @param prod
     * @return
     */
    @Override
    @Transactional
    public boolean save(Prod prod) {
        // 设置一些默认值
        prod.setCreateTime(new Date());
        prod.setUpdateTime(new Date());
        if(prod.getStatus().equals(1)){
            prod.setPutawayTime(new Date());
        }
        prod.setVersion(0);
        prod.setShopId(1L);
        prod.setSoldNum(0);
        // 拿到配置的对象
        Prod.DeliveryModeVo deliveryModeVo = prod.getDeliveryModeVo();
        prod.setDeliveryMode(JSON.toJSONString(deliveryModeVo));
        //写prod表
        int i = prodMapper.insert(prod);
        if(i > 0){
            handleSku(prod.getProdId(), prod.getSkuList());
            handleTagReference(prod.getProdId(), prod.getTagList());
        }
        return false;
    }

    /**
     * 写tag_reference表
     * @param prodId
     * @param tagList
     */
    private void handleTagReference(Long prodId, List<Long> tagList) {
        if(CollectionUtils.isEmpty(tagList)){
            return;
        }
        ArrayList<ProdTagReference> prodTagReferences = new ArrayList<>(tagList.size());
        tagList.forEach(tagId -> {
            ProdTagReference prodTagReference = new ProdTagReference();
            prodTagReference.setCreateTime(new Date());
            prodTagReference.setProdId(prodId);
            prodTagReference.setTagId(tagId);
            prodTagReference.setShopId(1L);
            prodTagReference.setStatus(true);
            prodTagReferences.add(prodTagReference);
        });
        prodTagReferenceService.saveBatch(prodTagReferences);
    }

    /**
     * 写sku表
     * @param prodId
     * @param skuList
     */
    private void handleSku(Long prodId, List<Sku> skuList) {
        if(CollectionUtils.isEmpty(skuList)){
            return;
        }
        skuList.forEach(sku -> {
            sku.setProdId(prodId);
            sku.setVersion(0);
            sku.setUpdateTime(new Date());
            sku.setRecTime(new Date());
            sku.setStatus(1);
            sku.setActualStocks(sku.getStocks());
        });
        skuService.saveBatch(skuList);
    }
}
